﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

using DM_Article.Bll;
using DM_Article.Model;

public partial class admin_admin_smallclassadd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script language='javascript'>alert('非法操作，请先登录！');location.href = 'admin_login.aspx';</script>");
            }
            else if (!checkUserType(Session["username"].ToString()))
            {
                Response.Write("<script language='javascript'>alert('权限不足！');location.href = 'admin_class.aspx';</script>");
            }
            else
            {
                getdlbigclass();
            }
        }      
    }

    //权限管理
    private bool checkUserType(string username)
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            cn.Open();
            admin adn = adminbll.getAdminDetailByName(cn, username);
            return (adn.usertype == "0" || adn.usertype == "1");
        }
    }

    private void getdlbigclass()
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            this.dlbigclass.DataSource = bigclassbll.getBigclassTable(cn).DefaultView;
            this.dlbigclass.DataTextField = "bigclassname";
            this.dlbigclass.DataValueField = "bigclassid";
            this.dlbigclass.DataBind();
        }
    }

    protected void btn_smallclassadd_Click(object sender, EventArgs e)
    {
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()))
        {
            cn.Open();
            string smallclassname = this.txtsmallclassname.Text;
            int bigclassid = Convert.ToInt32(this.dlbigclass.SelectedValue);
            smallclass sc = new smallclass();
            sc.smallclassname = smallclassname;
            sc.bigclassid = bigclassid;
            if (smallclassname == "")
            {
                Response.Write("<script language='javascript'>alert('请先填写小类名称！');</script>");
                this.txtsmallclassname.Focus();

            }
            else
            {
                if (smallclassbll.add(cn, sc) > 0)
                {
                    Response.Write("<script language='javascript'>alert('添加成功！');location.href = 'admin_smallclassadd.aspx';</script>");
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('添加失败！');location.href = 'admin_smallclassadd.aspx';</script>");
                }
            }
        }
    }
    protected void btn_back_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin_class.aspx");
    }
}
